package com.maven.front.controller.manageLogin;

import com.maven.front.aop.SysLog;
import com.maven.front.bean.*;
import com.maven.front.service.manageLogin.SysLoginService;
import com.maven.front.util.MD5;
import org.apache.log4j.net.SyslogAppender;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@CrossOrigin
@RestController
@RequestMapping("admin/sysUser")
public class SysLoginController {

    @Resource
    private SysLoginService sysLoginService;

    @Autowired
    private RedisTemplate redisTemplate;
     //登录
    @PostMapping("login")
    public Result  login(SysUser sysUser){
        String pwd=MD5.string2MD5(sysUser.getLoginPwd());
        sysUser.setLoginPwd(pwd);
        SysUser sysUser1=this.sysLoginService.login(sysUser);
        if(ObjectUtils.isEmpty(sysUser1)){
            return new Result(201, "用户名或者密码错误");
        }else {
            if (sysUser1.getStatus()==1) {
                return new Result(202,"您已被冻结，请联系管理员");
            } else {
                String token2 = UUID.randomUUID().toString();
                this.redisTemplate.opsForValue().set(token2, sysUser1);
                this.redisTemplate.expire(token2, 15, TimeUnit.MINUTES);
                return new Result(200, "成功", token2);
            }
        }
    }



      //查询所有数据/模糊查询
       @GetMapping("list")
        public Result list(String keyWord){
           List<SysUser> list=this.sysLoginService.list(keyWord);
           if(CollectionUtils.isEmpty(list)){
               return new Result(301,"无数据");
           }else{
               for(int i=0; i<list.size(); i++){
                   if (list.get(i).getFlag()==1){
                       list.remove(i);
                   }
               }
               return new Result(200,"成功",list);
           }
        }

        //查询单条数据
      @GetMapping("init")
      public Result init(String id){
        SysUser sysUser=this.sysLoginService.getById(Integer.valueOf(id));
        if(ObjectUtils.isEmpty(sysUser)){
            return new Result(301,"无数据");
        }else{
            return new Result(200,"success",sysUser);
        }

      }
     //新增
        @PostMapping("insert")
        public Result insert(SysUser sysUser){
            String pwd=MD5.string2MD5(sysUser.getLoginPwd());
            sysUser.setLoginPwd(pwd);
          int  res=this.sysLoginService.insert(sysUser);
          if(res>0){
              return new Result(200,"success",true);
          }else{
              return new Result(401,"添加失败");
          }
        }

        //删除
    @PostMapping("del")
    public Result del(String id){
        int res=this.sysLoginService.delete(Integer.valueOf(id));
        if(res>0){
            return new Result(200,"删除成功",true);
        }else{
            return new Result(501,"删除失败");
        }
    }
    //更新
    @PostMapping("update")
    public Result update(SysUser sysUser){
        int res=this.sysLoginService.update(sysUser);
        if(res>0){
            return new Result(200,"成功",true);
        }else{
            return new Result(601,"更新失败");
        }
    }

     //修改密码
    @PostMapping("updatePwd")
     public Result updatePwd(SysUser sysUser){
        String pwd=MD5.string2MD5(sysUser.getLoginPwd());
        sysUser.setLoginPwd(pwd);
        System.out.println(sysUser.getLoginPwd()+"-----------"+sysUser.getUserId());
        int res=this.sysLoginService.updatePwd(sysUser);
        if(res>0){
            return new Result(200,"修改成功",true);
        }else{
            return new Result(701,"修改失败");
        }

     }

     @PostMapping("upPassword")
       public Result upPassword(Integer id){
        int res=this.sysLoginService.upPassword(id);
        if(res>0){
            return new Result(200,"删除成功",true);
        }else{
            return new Result(201,"删除失败");
        }
       }

        //根据loginName查询个人信息
     @GetMapping("selectName")
    public Result selectName(String name){
        SysUser sysUser=this.sysLoginService.selectName(name);
        if(ObjectUtils.isEmpty(sysUser)){
            return new Result(201,"无数据");
        }else{
            return new Result(200,"success",sysUser);
        }
     }

     //修改冻结状态
    @PostMapping("updateStatus")
    public Result updateStatus(SysUser sysUser){
        int res=this.sysLoginService.updateStatus(sysUser);
        if(res>0){
            return new Result(200,"修改成功",true);
        }else{
            return new Result(801,"修改失败");
        }
     }

     //查看日志
      @RequestMapping("selectLogin")
     public Result selectLogin(@RequestBody LinkedHashMap<String, Integer> parem){
           int id = parem.get("id");
          System.out.println("id："+id);
          List<SysLoginLog> sysLoginLog=this.sysLoginService.selectLogin(id);
          if(ObjectUtils.isEmpty(sysLoginLog)){
              return new Result(1001,"无数据");
          }else{
              return new Result(200,"success",sysLoginLog);
          }
     }

      //查询role表
    @GetMapping("listAll")
    public Result listAll(SysRole sysRole){
          List<SysRole> list=this.sysLoginService.listAll(sysRole);
          if(CollectionUtils.isEmpty(list)){
              return new Result(2001,"无数据");
          }else{
              return new Result(200,"success",list);
          }
    }

     //查看系统日志
    @GetMapping("selectSyslog")
     public Result selectSyslog(){
        List<SysLog> list=this.sysLoginService.selectSyslog();
        if(CollectionUtils.isEmpty(list)){
            return new Result(202,"无数据");
        }else{
            return new Result(200,"success",list);
        }
     }
}
